package com.device.controller;

import com.alibaba.fastjson.JSONObject;
import com.device.annotation.LoginUser;
import com.device.dataobject.vo.ResultVO;
import com.device.entity.*;
import com.device.enums.Operation;
import com.device.repository.*;
import com.device.service.AdminService;
import com.device.util.ResultVOUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.NoSuchElementException;
import java.util.concurrent.CompletionException;

@RestController
@AllArgsConstructor
@RequestMapping("/admin")
public class AdminController {

    private final UserRepository userRepository;

    private final AdminService adminService;



    @PostMapping("/device")
    public ResultVO manageDevice(@LoginUser Integer userId, @RequestBody JSONObject jsonObject) {
        if (userId == null) {
            return ResultVOUtil.returnFail(401, "未提供有效的用户ID");
        }

        User user = userRepository.findById(userId)
                .orElseThrow(() -> new NoSuchElementException("用户不存在"));

        if (!user.getIsAdmin()) {
            return ResultVOUtil.returnFail(401, "没有权限");
        }

        String op = jsonObject.getString("op");
        Operation operation = Operation.valueOf(op.toUpperCase());

        switch (operation) {
            case EDIT:
                adminService.handleEditOperation(jsonObject);
                break;
            case ADD:
                adminService.handleAddOperation(jsonObject);
                break;
            case DELETE:
                adminService.handleDeleteOperation(jsonObject);
                break;
            case EDIT_UD:
                adminService.handleEditUDOperation(jsonObject,userId);
                break;
            case ADD_UD:
                adminService.handleAddUDOperation(jsonObject,userId);
                break;
            case DELETE_UD:
                adminService.handleDeleteUDOperation(jsonObject);
                break;
            case ALL_UD:
                return adminService.handleAllUDOperation(jsonObject).join();
            default:
                return ResultVOUtil.returnFail(400, "无效的操作");
        }

        return ResultVOUtil.returnSuccess();
    }


    @PostMapping("/user")
    public ResultVO manageUser(@LoginUser Integer userId, @RequestBody JSONObject jsonObject) {
        if (userId == null) {
            return ResultVOUtil.returnFail(401, "未提供有效的用户ID");
        }

        User user = userRepository.findById(userId)
                .orElseThrow(() -> new NoSuchElementException("用户不存在"));

        if (!user.getIsAdmin()) {
            return ResultVOUtil.returnFail(401, "没有权限");
        }

        String op = jsonObject.getString("op");
        Operation operation = Operation.valueOf(op.toUpperCase());

        switch (operation) {
            case DELETE:
                adminService.handleDeleteUserOperation(jsonObject);
                break;
            case PASS:
                adminService.handlePassUserOperation(jsonObject);
                break;
            case CANCEL_P:
                adminService.handleCancelPUserOperation(jsonObject);
                break;
            case CANCEL_A:
                adminService.handleCancelAUserOperation(jsonObject);
                break;
            case ADMIN:
                adminService.handleAdminUserOperation(jsonObject);
                break;
            case REGISTERING:
                return adminService.handleRegisteringOperation(jsonObject).join();

            case REGISTERED:
                return adminService.handleRegisteredOperation(jsonObject).join();

            case UNREGISTERED:
                return adminService.handleUnregisteredOperation(jsonObject).join();

            case COUNT:
                return adminService.handleCountOperation().join();

            default:
                return ResultVOUtil.returnFail(400, "无效的操作");
        }

        return ResultVOUtil.returnSuccess();
    }


    @PostMapping("/post")
    public ResultVO uploadDevice(@LoginUser Integer userId, @RequestBody JSONObject jsonObject) {
        if (userId == null) {
            return ResultVOUtil.returnFail(401, "未提供有效的用户ID");
        }

        User user = userRepository.findById(userId)
                .orElseThrow(() -> new NoSuchElementException("用户不存在"));

        if (!user.getIsAdmin()) {
            return ResultVOUtil.returnFail(401, "没有权限");
        }

        String op = jsonObject.getString("op");
        Operation operation = Operation.valueOf(op.toUpperCase());

        switch (operation) {
            case REJECT:
                adminService.handleRejectPostOperation(jsonObject);
                break;
            case APPROVE:
                return adminService.handleApprovePostOperation(jsonObject);
            case ALL:
                try {
                    return adminService.handleAllPostOperation(jsonObject).join();
                } catch (CompletionException e) {
                    // 异常处理
                    e.printStackTrace();
                    return ResultVOUtil.returnFail();
                }
            case EDIT:
               adminService.handleEditPostOperation(jsonObject);
            default:
                return ResultVOUtil.returnFail(400, "无效的操作");
        }

        return ResultVOUtil.returnSuccess();
    }

    @PostMapping("/rent")
    public ResultVO rentDevice(@LoginUser Integer userId, @RequestBody JSONObject jsonObject) {
        if (userId == null) {
            return ResultVOUtil.returnFail(401, "未提供有效的用户ID");
        }

        User user = userRepository.findById(userId)
                .orElseThrow(() -> new NoSuchElementException("用户不存在"));

        if (!user.getIsAdmin()) {
            return ResultVOUtil.returnFail(401, "没有权限");
        }

        String op = jsonObject.getString("op");
        Operation operation = Operation.valueOf(op.toUpperCase());
        switch (operation) {
            case REJECT:
                adminService.handleRejectRentOperation(jsonObject);
                break;
            case APPROVE:
                return adminService.handleApproveRentOperation(jsonObject);
            case ALL:
                try {
                    return adminService.handleAllRentOperation(jsonObject).join();
                } catch (CompletionException e) {
                    // 异常处理
                    e.printStackTrace();
                    return ResultVOUtil.returnFail();
                }
            case EDIT:
                 adminService.handleEditRentOperation(jsonObject);
            default:
                return ResultVOUtil.returnFail(400, "无效的操作");
        }

        return ResultVOUtil.returnSuccess();
    }

}

